<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, i) in goodsList" :key="i" @click="gotuDetail(goods)">
        <my-goods :goods= "goods"></my-goods>
      </view>
      
    </view>
  
  </view>
</template>

<script>
//import { render } from "vue";
  export default {
    data() {
      return {
        //请求参数对象
        queryObj:{
          //查询关键词
          query : '',
          //商品id
          cid : '',
          //页码值
           pagenum : 1,
          //每页显示数量
          pagesize : 10
        },
        //商品列表数据
        goodsList:[],
        //总数量
        total: 0,
        //节流阀
        isloading: false
       
        
      };
    },
    methods:{
      async getGoodsList(cd){
        
        this.isloading=true
        const { data:res } = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        //调用cd回调函数
        cd && cd()
        if(res.meta.status !==200) return uni.$showMsg()
        //为数据赋值
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
        this.isloading= false
      },
      gotuDetail(goods){
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id='+goods.goods_id
        })
      }
    },
   
    onLoad(options){
      //将页面参数转存this.queryObj对象中
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    //上拉加载
    onReachBottom(){
      //判断是否还有下一页数据
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total) return uni.$showMsg('没有了。。。')
      //判断节流阀是否开启
      if(this.isloading) return
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    //下拉刷新
    onPullDownRefresh(){
      //重置数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []
      //发起请求
      this.getGoodsList(()=>uni.stopPullDownRefresh())

      
    }
  }
</script>

<style lang="scss">

</style>
